package com.draby.starfantasymaze.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.dto.Achievement;

public class ToastUtil {
    private static Toast toast;

    /**
     * 显示一条指定信息的 toast 提示
     * @param context context
     * @param msg 显示的信息
     */
    public static void showMsg(Context context, String msg) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    /**
     * 显示完成的成就信息
     * @param context context
     * @param achievement 完成的成就
     */
    public static void showAchievement(Context context, Achievement achievement) {
        Toast achievementToast = new Toast(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_achievement_item, null);
        // 设置视图的缩放比例
        view.setScaleX(0.8f);
        view.setScaleY(0.8f);
        // 找到视图中的控件
        TextView tv_finish = view.findViewById(R.id.tv_finish);
        ImageView iv_achievement = view.findViewById(R.id.iv_achievement);
        TextView tv_achievement_name = view.findViewById(R.id.tv_achievement_name);
        TextView tv_achievement_resume = view.findViewById(R.id.tv_achievement_resume);
        // 设置这些控件的属性
        tv_finish.setVisibility(View.VISIBLE);
        if (achievement.getHidden()) {
            tv_finish.setText("解锁成就");
        } else {
            tv_finish.setText("成就完成");
        }
        iv_achievement.setImageResource(achievement.getPictureID());
        tv_achievement_name.setText(achievement.getName());
        tv_achievement_resume.setText(achievement.getResume());
        achievementToast.setView(view);
        achievementToast.setDuration(Toast.LENGTH_LONG);
        achievementToast.show();
    }
}
